﻿using PerfAssist.CoroutineTracker;
using PerfAssist.Demo.CoroutineTracker;
using PerfAssist.Demo.CoroutineTrackerDemo;
using UnityEditor;
using UnityEngine;
#if UNITY_EDITOR

#endif

namespace PerfAssist.CoroutineTrackerDemo
{
    public class SampleRunner : MonoBehaviour
    {
        private void SendEditorCommand(string cmd)
        {
#if UNITY_EDITOR
            var w = EditorWindow.GetWindow<EditorWindow>("CoTrackerWindow");
            if (w.GetType().Name == "CoTrackerWindow") w.SendEvent(EditorGUIUtility.CommandEvent(cmd));
#endif
        }

        private void Start()
        {
            // bootstrapping
            CoroutineRuntimeTrackingConfig.EnableTracking = true;
            StartCoroutine(RuntimeCoroutineStats.Instance.BroadcastCoroutine());

            SendEditorCommand("AppStarted");

            gameObject.AddComponent<TestPluginRunner>();

            CoroutinePluginForwarder.InvokeStart_IEnumerator = RuntimeCoroutineTracker.InvokeStart;
            CoroutinePluginForwarder.InvokeStart_String = RuntimeCoroutineTracker.InvokeStart;

            var spawner = gameObject.AddComponent<CoroutineSpawner>();
            RuntimeCoroutineTracker.InvokeStart(spawner, "Co01_WaitForSeconds");
            RuntimeCoroutineTracker.InvokeStart(spawner, "Co02_PerFrame_NULL");
            RuntimeCoroutineTracker.InvokeStart(spawner, "Co03_PerFrame_EOF");
            RuntimeCoroutineTracker.InvokeStart(spawner, "Co04_PerFrame_ARG", 0.683f);
        }

        private void OnDestroy()
        {
            SendEditorCommand("AppDestroyed");
        }
    }
}